//import java.util.*;
import java.io.*;

public class Restaurant {
	
	public static void main(String[] args){
		String url = new String("D://Eclipse35/Java_MyFile/XMLTest/Classified_Corpus.xml");
		//int counter = 0;
		//int previous_counter = 0;
		boolean flag = false;
		BufferedReader reader = null;
		BufferedWriter writer = null;
		
		try {
			reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(url)));
			String line = new String();
			String[] terms = null;
			String newFileName = null;;
			
			while(( line = reader.readLine()) != null){
				line.trim();	
				if(line.contains("<root>") || line.contains("</root>"))
					continue;
				// this line contains Restaurant id --> means a new restaurant's data comes
				
				if(line.contains("<Restaurant id=")){
					//if (flag == false)
						//flag = true;
					
					terms = line.split("xmlns");
					System.out.println("***This line contains Restaurant id!***");
					System.out.println(terms[0].substring(16, terms[0].length()-2));
					
					newFileName = new String(terms[0].substring(16, terms[0].length()-2)) + ".xml";
					writer = new BufferedWriter(new FileWriter("D://Eclipse35/Java_MyFile/XMLTest/files/"+newFileName));
					writer.write(line+'\n');
				}
							
				
				else{					
					//System.out.println(line);
					//if(flag == true)						
					writer.write(line+'\n');	
					if(line.contains("</Restaurant>"))
						writer.flush();					
				}
				
			}
			reader.close();
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
}
